import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import index from '../views/index.vue'

import {
  Toast
} from 'vant'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'index',
    component: index,
    redirect: 'home',
    children: [{
      path: 'home',
      name: 'home',
      component: HomeView,

    }, {
      path: 'goods',
      name: 'goods',
      component: () => import('../views/goods.vue')
    }, {
      path: 'my',
      name: 'my',
      component: () => import('../views/my.vue')
    }],
    meta: {
      keep: true
    }
  },
  {
    path: '/city',
    name: 'city',
    component: () => import('../views/city.vue'),
    meta: {
      keep: true
    }
  },
  {
    path: '/shopList',
    name: 'shopList',
    component: () => import('../views/shopList.vue'),
    meta: {
      keep: false
    }
  },
  {
    path: '/filmsIntro',
    name: 'filmsIntro',
    component: () => import('../views/filmsIntro.vue'),
    meta: {
      keep: true
    }
  },
  {
    path: '/filmsOrder',
    name: 'filmsOrder',
    component: () => import('../views/filmsOrder.vue'),
    meta: {
      keep: false
    }
  },
  {
    path: '/cinema',
    name: 'cinema',
    component: () => import('../views/cinema.vue'),
    meta: {
      keep: false
    }
  },
  {
    path: '/seat',
    name: 'seat',
    component: () => import('../views/seat.vue'),
    meta: {
      keep: false
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/reg',
    name: 'reg',
    component: () => import('../views/reg.vue')
  },
  {
    path: '/userInfo',
    name: 'userInfo',
    component: () => import('../views/userInfo.vue'),
    meta: {
      auth: true
    }
  },
  {
    path: '/pay',
    name: 'pay',
    component: () => import('../views/pay.vue'),
    meta: {
      auth: true
    }
  },
  {
    path: '/paySuccess',
    name: 'paySuccess',
    component: () => import('../views/paySuccess.vue'),
    meta: {
      auth: true
    }
  }
]

const router = new VueRouter({
  routes
});

//   有些页面  设置的有权限  没权限情况下不让进  
//  我们要实现这个拦截功能  需要在路由里拦截  

// 全局拦截   

// 哪个路由里有auth 属性 true  表示他需要被验证  
// 如果没有的  证明不用验证     那些基础页面 首页  登录页等等  

//  一些页面需要验证   

router.beforeEach((to, from, next) => {
  // to  到哪里去
  // from   从何而来
  // next()   后续操作

  if (to.meta.auth) {
    //  他是需要验证的  
    let token = localStorage.getItem('xl-token')
    let uid = localStorage.getItem('xl-uid');

    if (uid && token) {
      //  如果 自己是登录状态 
      next()
    } else {

      Toast('请先登录')
      router.replace('/login')
    }



  } else {

    next()
  }

})




// 页面级拦截



export default router